﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="ChangePassword.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModels.Common;
using GenericTools;

namespace ReportingManagement
{
    /// <summary>
    /// Class for changing user password
    /// </summary>
    public partial class ChangePassword : Form
    {
        #region Class Variables
        private UserInformation user;
        private bool modified;
        #endregion

        #region Constructors
        public ChangePassword(UserInformation user)
        {
            InitializeComponent();
            this.user = user;
            this.modified = false;
            this.Text = String.Format("Changing User Password - {0}", this.user.FullName);
        }
        #endregion

        #region Public Properties
        public UserInformation ModifiedUserInformation
        {
            get
            {
                if (modified) return this.user;
                else return null;
            }
        }
        #endregion

        #region Event Handlers
        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            #region Validations
            if (String.IsNullOrEmpty(txtCurrentPassword.Text))
            {
                MessageBox.Show(this, "Please provide your current password",
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCurrentPassword.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtNewPassword.Text))
            {
                MessageBox.Show(this, "Please provide your new password",
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNewPassword.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtNewPasswordConfirm.Text))
            {
                MessageBox.Show(this, "Please provide your new password confirmation",
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNewPasswordConfirm.Focus();
                return;
            }

            if (txtNewPassword.Text != txtNewPasswordConfirm.Text)
            {
                MessageBox.Show(this, "Please make sure new password and its confirmation match",
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNewPasswordConfirm.Focus();
                return;
            }

            //if (GenericTools.DataTransformation.Hash(txtCurrentPassword.Text) != user.Password)
            if (txtCurrentPassword.Text != user.Password)
            {
                MessageBox.Show(this, "Please provide correct current password",
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCurrentPassword.Focus();
                return;
            }
            #endregion

            string oldPassword = txtCurrentPassword.Text;
            string newPassword = txtNewPassword.Text;
            bool success = false;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                success = user.ChangePassword(oldPassword, newPassword);
                Cursor.Current = Cursors.Default;
                if (success)
                {
                    this.modified = true;
                    MessageBox.Show(this, "You have successfully changed your password", 
                        AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.modified = false;
                    MessageBox.Show(this, "Unknown error has occured. Changing password could not complete",
                        AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                    this.Close();
                }
            }
            catch
            {
                this.modified = false;
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Please make sure you have internet connection", AppConfiguration.ApplicationName,
                   MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
        }
        #endregion
    }
}
